/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package input;

import graphic.Graphic;
import objects.SRect;
import objects.Text2dPrinter;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Color;

/**
 *
 * @author Marcin
 */
public class InputBox {
    public String txt="";
    public boolean act=true;
    public boolean pass=false;
    public boolean background=false;
    public SRect rect = new SRect();
    int x,y;
    public Text2dPrinter wr = new Text2dPrinter();
    public InputBox(int x1,int x2,int y1,int y2){
        rect.set(x1, x2, y1, y2);
        x=x1;
        y=y1;
        wr.setFont("Alan Den.ttf",20);
        wr.setColor(Color.white);
        Keyboard.enableRepeatEvents(true);
    }
    
    public String gen(){
        if(!pass) return txt;
        String gen="";
        for(int i=0;i<txt.length();++i)
            gen+="*";
        return gen;
    }
    
    public void refresh(){
        
        if(act){
            while(Keyboard.next()){
                
                if(Keyboard.getEventKey()==Keyboard.KEY_BACK){
                    if(txt.length()>0 && Keyboard.getEventCharacter()!=0)
                        txt=txt.substring(0, txt.length()-1);
                }else{
                    int i=Keyboard.getEventCharacter();
                    char c=(char)i;
                    if(c!=0) txt+=c;
                }
            }
        }
        Graphic.e.unbind();
        if(act) Graphic.e.setColor(new graphic.Color(0,0,1,1));
        else Graphic.e.setColor(new graphic.Color(1,1,1,1));
        rect.background=background;
        rect.draw2d();
        if(!act || System.nanoTime()%1000000000L>500000000L){
            wr.write(x, y, gen());
        }
        else{
            wr.write(x, y, gen()+"/");
        }
    }
    
}
